﻿
<html>
<head>
    <link href="/Scripts/layui/css/layui.css" rel="stylesheet" />
</head>

<body>
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 10px;">
            <legend>本页面接口测试,{layim}为默认startup中的配置前缀，可以改成自定义路径</legend>
        
    </fieldset>
    <table class="layui-table" layeven="" layskin="nob">
        
        <colgroup>
            
            <col width="150">
            
            <col width="150">
            
            <col width="200">
            
            <col>
            
        </colgroup>
        
        <thead>
            
            <tr>
                
                <th>接口名称</th>
                
                <th>url</th>
                
                <th>请求方式</th>
                
                <th>参数要求</th>
                
                <th>试一试</th>
                
            </tr>
            
        </thead>
        
        <tbody>
            
            <tr>
                
                <td>添加群组</td>
                
                <td>/{layim}/group/add</td>
                
                <td>POST</td>
                
                <td>当前用户ID（uid）<br />群组名称（name）<br />群组描述(desc)<br />群组图片(avatar)</td>
                
                <td><button class="layuibtn layuibtnprimary layuibtnsmall" apievent="groupadd">试一试</button></td>
                
            </tr>
            
            <tr>
                
                <td>添加用户</td>
                
                <td>/{layim}/user/add</td>
                
                <td>POST</td>
                
                <td>用户昵称（name）<br />用户头像（avatar）<br />签名(sign)</td>
                
                <td><button class="layuibtn layuibtnprimary layuibtnsmall" apievent="useradd">试一试</button></td>
                
            </tr>
            
            <tr>
                
                <td>申请好友</td>
                
                <td>/{layim}/friend/apply</td>
                
                <td>POST</td>
                
                <td>申请人ID(uid)<br />被申请人ID(tid)<br />备注(other)</td>
                
                <td><button class="layuibtn layuibtnprimary layuibtnsmall" apievent="friendapply">试一试</button></td>
                
            </tr>
            
            <tr>
                
                <td>申请加群</td>
                
                <td>/{layim}/group/apply</td>
                
                <td>POST</td>
                
                <td>申请人ID(uid)<br />群组ID(tid)<br />备注(other)</td>
                
                <td><button class="layuibtn layuibtnprimary layuibtnsmall" apievent="groupapply">试一试</button></td>
                
            </tr>
            
            <tr>
                
                <td>用户查找</td>
                
                <td>/{layim}/user/search</td>
                
                <td>GET</td>
                
                <td>起始序号(start)<br />条数(num)<br />关键词.目前只支持昵称(key)<br />排序按照注册时间正序</td>
                
                <td><button class="layuibtn layuibtnprimary layuibtnsmall" apievent="usersearch">试一试</button></td>
                
            </tr>
            
            <tr>
                
                <td>群组查找</td>
                
                <td>/{layim}/group/search</td>
                
                <td>GET</td>
                
                <td>起始序号(start)<br />条数(num)<br />关键词.目前只支持群名(key)<br />排序按照添加时间正序</td>
                
                <td><button class="layuibtn layuibtnprimary layuibtnsmall" apievent="groupsearch">试一试</button></td>
                
            </tr>
            
        </tbody>
        
    </table>
    
    <script src="/Scripts/layui/layui.js"></script>
    
    <script>
        var prefix = "/layim";
        layui.use(['jquery', 'layer'], function ($, l) {
            var layer = l;
            $(function () {
                $('[apievent]*').on('click', function (e) {
                    var event = $(this).attr('apievent');
                    test.request(event);
                });
            });


            var api = [
                { name: '添加群组', func: 'groupadd', url: '/group/add', params: ['uid', 'name', 'desc', 'avatar'], type: 'POST' },
                { name: '添加用户', func: 'useradd', url: '/user/add', params: ['name', 'avatar', 'sign'], type: 'POST' },
                { name: '申请好友', func: 'friendapply', url: '/friend/apply', params: ['uid', 'tid', 'other'], type: 'POST' },
                { name: '申请加群', func: 'groupapply', url: '/group/apply', params: ['uid', 'tid', 'other'], type: 'POST' },
                { name: '用户查找', func: 'usersearch', url: '/user/search', params: ['start', 'num', 'key'], type: 'GET' },
                { name: '群组查找', func: 'groupsearch', url: '/group/search', params: ['start', 'num', 'key'], type: 'GET' }
            ];

            var test = {
                request: function (o) {
                    var detail = api.filter(function (item) {
                        return item.func == o
                    })[0];

                    var html = (function () {
                        var t = '<div>';
                        for (var i = 0; i < detail.params.length; i++) {
                            t += '<input type="text" name="' + o + '" data-param="' + detail.params[i] + '" placeholder="' + detail.params[i] + '" class="layui-input" style="margin:2px;">'
                        }
                        t += '</div>';
                        return t;
                    })();
                    var url = prefix + detail.url;
                    var d = function (func) {
                        var data = {};
                        $('input[name="' + func + '"]').each(function () {
                            var p = $(this).data('param'), val = $(this).val();
                            data[p] = val;
                        })
                        return data;
                    };
                    layer.confirm(html, {
                        title: detail.name
                   , shade: [0.8, '#393D49'],
                        yes: function (index) {
                            $.ajax({
                                url: url,
                                type: detail.type,
                                data: d(o),
                                success: function (res) {
                                    console.log(res);
                                    layer.msg(res.msg||'请查看console');
                                }
                            });
                        }
                    });
                }
            };

        });
    </script>
    
</body>

</html>